#include <unistd.h>
#include <sys/stat.h>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

int main( int argc, char *argv[] )
{
	time_t now;
	struct timeval now_val;

	now = time( NULL );
	gettimeofday( &now_val, NULL );

	cout << "time(): " << now << endl;
	cout << "gettimeofday(), tv_sec: " << now_val.tv_sec << " tv_usec: " << now_val.tv_usec << endl;

	struct tm *t;

	t = localtime( &now );
	// t = gmtime( &now );
	cout << "year of struct tm: " << t->tm_year << endl; // from 1900
	cout << "ctime(time_t *): " << ctime( &now ) << endl;
	cout << "asctime(struct tm * ): " << asctime( t ) << endl;

	char timestr[255];
	strftime( timestr, 255, "%Y-%m-%d %H:%M:%S", t );
	cout << "format output strftime(): " << timestr << endl;

	// free(t); //不能 free or delete 不然立马出错。 ？？？
	// from man gmtime:  The return value points to a statically allocated struct which might be overwritten by subsequent calls to any of the date and time functions.

	return 0;
}

// 除了gettimeofday()的都是由ISO C定义的。
